package simulator;

public class SyncBufferCache extends BufferCache {
	
	public SyncBufferCache(int numFrames)
	{
		super(numFrames);
	}
	
	public void updateBlock(String fileName, int data)
	{
		//lazyCommit();
		int frameNo = findBlock(fileName);
		
		if(frameNo >= 0){
			hits++;
		}
		
		else{
		//Cache miss. For now, let's bring it into the buffer cache
			misses++;
			frameNo = readIn(fileName); //read in the data from the specified file of the frame.
		//this is the index of our new frame of data
		}
		
		Frame f = frames[frameNo];

		f.setData(data);
		ja.commitSync(f.block);
		f.isDirty = false;
		f.timeStamp = ss.getTime();

	}
	
	/**
	 * Sync policy: commit immediately when block becomes dirty
	 * @param b
	 */
	public void synCommit(Frame f)
	{
			evictDirty(f.block);
			f.isDirty = false;
	}
	
	public void commitAll()
	{
		return;
	}
	
	public void evictDirty(Block b)
	{
		ja.commitData(b);
	}

	@Override
	public void flushDirty() {
		// TODO Auto-generated method stub
		commitAll();
		ja.checkpointAll();
	}

	@Override
	public void checkpointAll() {
		// TODO Auto-generated method stub
		return;
	}
	
	@Override
	public String SimString() {
		// TODO Auto-generated method stub
		String str = "Sim type: Sync Journaling\nHits: " + hits + "\nMisses: " + misses;
		return str;
	}
}
